<template>
  <el-card class="box-card" style="margin: 20px 0">
    <div slot="header" class="top">
      <!--  @tab-click="handleClick"  -->
      <!-- 头部左侧内容 -->
      <el-tabs v-model="activeName" class="tab">
        <el-tab-pane label="销售额" name="sale"> </el-tab-pane>
        <el-tab-pane label="访问量" name="visit"> </el-tab-pane>
      </el-tabs>
      <!-- 头部右侧内容 -->
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <span class="block">
          <el-date-picker
            v-model="date"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            class="date"
          >
          </el-date-picker>
        </span>
      </div>
    </div>
    <el-row :gutter="10">
      <!-- 销售额趋势 -->
      <el-col :span="18">
        <!-- 准备一个容器 -->
        <div class="barCharts" ref="barCharts"></div>
      </el-col>
      <!-- 门店排名 -->
      <el-col :span="6">
        <div class="col-right">
          <h4>门店{{ title }}排名</h4>
          <ul>
            <li v-for="(item, index) in ranking" :key="index">
              <span :class="index < 3 ? 'col-right-red' : ''">{{
                index + 1
              }}</span>
              <span class="col-right-m">{{ item.name }}</span>
              <span>{{ item.count }}</span>
            </li>
          </ul>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import * as dayjs from "dayjs";
export default {
  name: "Sale",
  data() {
    return {
      activeName: "sale",
      ranking: [
        { name: "肯德基", count: 13955 },
        { name: "麦当劳", count: 12331 },
        { name: "海底捞", count: 10987 },
        { name: "必胜客", count: 10882 },
        { name: "必胜客", count: 10882 },
        { name: "必胜客", count: 10882 },
        { name: "必胜客", count: 10882 },
      ],
      barCharts: "",
      // 日历
      date: "",
      mock1: [10, 52, 200, 334, 390, 330, 220, 24, 54, 76, 200, 56],
      mocl2: [1000, 502, 250, 374, 240, 930, 820, 684, 574, 765, 920, 356],
    };
  },
  computed: {
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
  },
  methods: {
    // 今日
    setDay() {
      let day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    // 本周
    setWeek() {
      let start = dayjs().day(1).format("YYYY-MM-DD");
      let end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    // 本月
    setMonth() {
      let start = dayjs().startOf("month").format("YYYY-MM-DD");
      let end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    // 本年
    setYear() {
      let start = dayjs().startOf("year").format("YYYY-MM-DD");
      let end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
  mounted() {
    // 实例化一个echarts
    this.barCharts = echarts.init(this.$refs.barCharts);
    // 配置数据
    this.barCharts.setOption({
      title: {
        text: "销售额趋势",
        textStyle: {
          fontSize: "14px",
        },
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [10, 52, 200, 334, 390, 330, 220, 24, 54, 76, 200, 56],
          color: "#9ab4cd",
        },
      ],
    });
  },
  watch: {
    title() {
      // 重新修改图表的数据
      this.barCharts.setOption({
        title: {
          text: this.title + "趋势",
        },
        series: [
          {
            data: this.title == "销售额" ? this.mock1 : this.mocl2,
          },
        ],
      });
    },
  },
};
</script>

<style scoped>
.top {
  display: flex;
  justify-content: space-between;
  position: relative;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.right span {
  font-size: 12px;
  margin-left: 10px;
}
.date {
  width: 230px;
}
.barCharts {
  width: 100%;
  height: 300px;
}
.col-right ul {
  list-style: none;
}
.col-right ul li {
  margin: 10px 0;
}
.col-right-red {
  color: red;
}
.col-right-m {
  margin: 0 30px;
}
</style>